package com.example.user_service.user_contorller;


import com.example.user_service.config.Idcred;
import com.example.user_service.return_data.Mall_Data;
import com.example.user_service.service.Gservice;
import com.example.user_service.vo.Administrator;
import com.netflix.discovery.converters.Auto;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import io.swagger.annotations.*;
import org.aspectj.apache.bcel.Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

@RestController
@CrossOrigin
@Api
@DefaultProperties(defaultFallback = "error")
public class User_contorller {

    @Autowired
    Gservice gservice;
    @Autowired
    Idcred idcred;
    @Autowired
    Mall_Data mall_data;


    @ApiOperation(value = "注册验证手机号是否被注册 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tel", value = "手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String")
    })
    @RequestMapping("/g_yz_tel")
    public Mall_Data g_yz_tel(String tel, String code) {
        //   Mall_Data mall_data = new Mall_Data();
        if (!gservice.iscode(tel, code)) {
            return mall_data.setcode(-1).seterror("验证错误");
        } else if (gservice.istel(tel)) {
            return mall_data.setcode(-2).seterror("已被注册");
        } else {
            return mall_data.setcode(1);
        }
    }

    @ApiOperation("调用发送手机短信")
    @ApiImplicitParam(name = "tel", value = "手机号", required = true, dataType = "String")
    @RequestMapping("/g_send_dx")
    public Mall_Data g_send_dx(String tel) {
        //    Mall_Data mall_data = new Mall_Data();
        System.out.println(tel);
        String getcode = gservice.getcode(tel);
        return null;
    }

    @ApiOperation("登录管理检验")
    @ApiImplicitParam(name = "实体类", dataType = "Administrator")
    @RequestMapping("/g_administrator_login")
    public Mall_Data g_administrator_login(Administrator administrator) {
        //  Mall_Data mall_data = new Mall_Data();
        return gservice.login_Administrator(administrator) ? mall_data.setcode(1) : mall_data.setcode(-1).seterror("密码或者账号错误");
    }

    @ApiOperation("校验身份证的合法性")
    @RequestMapping("/g_select_idcard")
    public Mall_Data select_idcard(@ApiParam("身份证") String idcard, @ApiParam("真实姓名") String name) {
        //0 一致，1 不一致，2 无记录
        Mall_Data mall_data = new Mall_Data();
        return mall_data.setcode(this.idcred.yzidcred(name, idcard));
    }

    @ApiOperation("校验账号是否是否重复")
    @RequestMapping("/g_select_name")
    public Mall_Data select_name(@ApiParam("账号") String name) {
        //1没有注册 -1被注册
        // Mall_Data mall_data = new Mall_Data();
        if (gservice.isaccount(name)) return mall_data.setcode(1);
        return mall_data.setcode(-1);
    }

    @ApiOperation("注册")
    @RequestMapping("/g_insert")
    public Mall_Data g_insert(@ApiParam("手机号") String tel, @ApiParam("真实姓名") String zsname, @ApiParam("身份证") String idcred, @ApiParam("账号") String account, @ApiParam("密码") String pawd) {
        //  Mall_Data mall_data = new Mall_Data();
        if (gservice.insert(tel, zsname, idcred, account, pawd)) return mall_data.setcode(1);
        return mall_data.setcode(-1);
    }

    @ApiOperation("密码登录验证")
    @RequestMapping("/g_login")
    public Mall_Data g_login(@ApiParam("账号") String name, @ApiParam("密码") String pawd) {
        // Mall_Data mall_data = new Mall_Data();
        int login = gservice.is_login(name, pawd);
        if (login != -1) {
            return mall_data.setcode(1).setdata(login);
        } else {
            return mall_data.setcode(-1);
        }
    }

    @ApiOperation("手机验证码验证")
    @RequestMapping("/g_tel")
    public Mall_Data g_tel(@ApiParam("手机号") String tel, @ApiParam("验证码") String code) {
        // Mall_Data mall_data = new Mall_Data();

        if (!gservice.istel(tel)) {
            return mall_data.setcode(-2).seterror("手机没注册");
        }
        if (!gservice.iscode(tel, code)) {
            return mall_data.setcode(-1).seterror("验证错误");
        } else {
            return mall_data.setcode(1).setdata(gservice.getuserid(tel));
        }
    }



    public Mall_Data error() {
        // Mall_Data mall_data = new Mall_Data();
        return mall_data.setcode(-111).seterror("服务挂了");
    }

}
